<template>
	<div class="content">
		<Collapse v-model="value1">
			<Panel name="1">
				奖池配置
				<p slot="content">
					<Form label-position="right" :label-width="100">
						<div class="canshu">
							<FormItem label="">
								发放层级
								<Input class="w80" v-model.trim="config.distributedHierarchical.content" :disabled="noconfig" /> 层
							</FormItem>
							<FormItem style="margin-left: -60px;">
								<i-switch size="large" v-model.trim="noconfig" @on-change="switchOpenAndClose">
									<span slot="open">全部</span>
									<span slot="close">配置</span>
								</i-switch>
							</FormItem>
							<div class="p-10">
								<Button type="primary" size="large" v-on:click="btn1(config.distributedHierarchical.id)" :disabled="noconfig">保　存</Button>
							</div>
						</div>
					</Form>
				</p>
				<p slot="content">
					<Form label-position="right" :label-width="100">
						<div class="canshu">
							<FormItem label="">
								每单让利
								<Input class="w80" v-model.trim="config.genlis.content" /> %
							</FormItem>
							<div class="p-10">
								<Button type="primary" size="large" v-on:click="btn2(config.genlis.id)">保　存</Button>
							</div>
						</div>
					</Form>
				</p>
				<p slot="content">
					<Form label-position="right" :label-width="100">
						<div class="canshu">
							<FormItem label="">
								发放周期
								<Input class="w80" v-model.trim="config.issueCycle.content" /> 天
							</FormItem>
							<div class="p-10">
								<Button type="primary" size="large" v-on:click="btn3(config.issueCycle.id)">保　存</Button>
							</div>
						</div>
					</Form>
				</p>
				<p slot="content">
					<Form label-position="right" :label-width="100">
						<div class="canshu">
							<FormItem label="">
								分享用户需有
								<Input class="w80" v-model.trim="config.numberAdvertisers.content" /> 个广告主
							</FormItem>
							<div class="p-10">
								<Button type="primary" size="large" v-on:click="btn4(config.numberAdvertisers.id)">保　存</Button>
							</div>
						</div>
					</Form>
				</p>
			</Panel>
		</Collapse>
	</div>

</template>
<script>
	export default {
		data() {
			return {
				noconfig: '', // 1.不能输入 2.能输入
				value1: '1', // 默认展开第一个列表
				config: { // 奖池配置
					distributedHierarchical: '', // 层数
					genlis: '', // 抽取百分比
					issueCycle: '', // 已发放天数
					numberAdvertisers: '', // 已推荐广告主
					all: ''
				}
			}
		},
		created() {

		},
		mounted() {
			this.$Message.config({
				top: 250
			})
			this.$nextTick(function() {
				this.getConfig(); // 查询奖池配置
			})
		},
		methods: {
			getConfig() { // 查询奖池配置
				let token = localStorage.getItem('mytoken');
				this.axios.get('parameter/pond/config', {
					headers: {
						Authorization: 'bearer ' + token
					}
				}).then(res => {
					if(res.data.code == 8008) {
						this.config = res.data.data;
						if(res.data.data.all.content == 1) {
							this.noconfig = true;
						} else if(res.data.data.all.content == 2) {
							this.noconfig = false;
						}
					} else if(res.data.code == 4004) {
						this.$Message.info(res.data.message);
					} else if(res.data.code == 5501) {
						this.$Message.info(res.data.message);
						localStorage.removeItem('mytoken');
						this.$router.push({
							name: 'login'
						})
					}
				})
			},
			btn1(index) { // 修改已设置的层数
				let token = localStorage.getItem('mytoken');
				let params = this.qs.stringify({
					id: index,
					content: this.config.distributedHierarchical.content
				})
				this.axios.put('parameter/pond/config', params, {
					headers: {
						Authorization: 'bearer ' + token
					}
				}).then(res => {
					if(res.data.code == 8008) {
						this.getConfig();
						this.$Message.info(res.data.message);
					} else if(res.data.code == 4004) {
						this.$Message.info(res.data.message);
					} else if(res.data.code == 5501) {
						this.$Message.info(res.data.message);
						localStorage.removeItem('mytoken');
						this.$router.push({
							name: 'login'
						})
					}
				})
			},
			btn2(index) { // 修改已收取的百分比
				let token = localStorage.getItem('mytoken');
				let params = this.qs.stringify({
					id: index,
					content: this.config.genlis.content
				})
				this.axios.put('parameter/pond/config', params, {
					headers: {
						Authorization: 'bearer ' + token
					}
				}).then(res => {
					if(res.data.code == 8008) {
						this.getConfig();
						this.$Message.info(res.data.message);
					} else if(res.data.code == 4004) {
						this.$Message.info(res.data.message);
					} else if(res.data.code == 5501) {
						this.$Message.info(res.data.message);
						localStorage.removeItem('mytoken');
						this.$router.push({
							name: 'login'
						})
					}
				})
			},
			btn3(index) { // 修改已发放的天数
				let token = localStorage.getItem('mytoken');
				let params = this.qs.stringify({
					id: index,
					content: this.config.issueCycle.content
				})
				this.axios.put('parameter/pond/config', params, {
					headers: {
						Authorization: 'bearer ' + token
					}
				}).then(res => {
					if(res.data.code == 8008) {
						this.getConfig();
						this.$Message.info(res.data.message);
					} else if(res.data.code == 4004) {
						this.$Message.info(res.data.message);
					} else if(res.data.code == 5501) {
						this.$Message.info(res.data.message);
						localStorage.removeItem('mytoken');
						this.$router.push({
							name: 'login'
						})
					}
				})
			},
			btn4(index) { // 修改多少个广告主
				let token = localStorage.getItem('mytoken');
				let params = this.qs.stringify({
					id: index,
					content: this.config.numberAdvertisers.content
				})
				this.axios.put('parameter/pond/config', params, {
					headers: {
						Authorization: 'bearer ' + token
					}
				}).then(res => {
					if(res.data.code == 8008) {
						this.getConfig();
						this.$Message.info(res.data.message);
					} else if(res.data.code == 4004) {
						this.$Message.info(res.data.message);
					} else if(res.data.code == 5501) {
						this.$Message.info(res.data.message);
						localStorage.removeItem('mytoken');
						this.$router.push({
							name: 'login'
						})
					}
				})
			},
			switchOpenAndClose(index) { // 开启和关闭
				let sureIndex = index;
				if(index == true){
					sureIndex = 1;
				}else if(index == false){
					sureIndex = 2;
				}
				let token = localStorage.getItem('mytoken');
				this.axios.get('parameter/get/advPoolOff/' + sureIndex, {
					headers: {
						Authorization: 'bearer ' + token
					}
				}).then(res => {
					if(res.data.code == 8008) {
						this.getConfig();
					} else if(res.data.code == 4004) {
						this.$Message.info(res.data.message);
					} else if(res.data.code == 5501) {
						this.$Message.info(res.data.message);
						localStorage.removeItem('mytoken');
						this.$router.push({
							name: 'login'
						})
					}
				})
			}
		}
	}
</script>
<style lang="less" scoped>
	.p-10 {
		padding-top: 0 !important;
		padding-left: 50px !important;
	}
	
	.w50 {
		width: 50px !important;
	}
	
	.mar {
		margin-left: 5px;
	}
</style>